//
//  KAUserInfoVerVC.swift
//  kano
//
//  Created by Frank on 2024/10/31.
//

import UIKit
import RxCocoa
import RxSwift
import JXSegmentedView

class KAUserInfoVerVC: KABaseVC {
    var status: Int = 0
    var buttonHandle: ((String, String) -> ())?
    
    private let disposeBag = DisposeBag()

    lazy var var_cornerView: UIView = {
        let var_cornerView = UIView()
        var_cornerView.backgroundColor = UIColor.white
        var_cornerView.layer.cornerRadius = kscale(30)
        var_cornerView.layer.masksToBounds = true
        var_cornerView.frame = CGRectMake(kscale(20), kscale(20), var_screenWidth-kscale(40), kscale(330))
        return var_cornerView
    }()
    
    lazy var var_titleLabel: UILabel = {
        let var_titleLabel = UILabel()
        var_titleLabel.x = kscale(20)
        var_titleLabel.y = kscale(32)
        var_titleLabel.width = var_cornerView.width-kscale(40)
        var_titleLabel.height = kscale(66)
        var_titleLabel.textAlignment = .left
        var_titleLabel.font = UIFont.systemFont(ofSize: kscale(20), weight: .regular)
        var_titleLabel.textColor = .black
        var_titleLabel.text = "To ensure the security of your account, please perform BVN verification"
        var_titleLabel.numberOfLines = 0
        var_titleLabel.sizeToFit()
        return var_titleLabel
    }()
    
    lazy var var_bvnView: KAVerCodeView = {
        var_bvnView = KAVerCodeView(placeHolder: "Please enter your BVN",limit: 11)
        var_bvnView.x = kscale(20)
        var_bvnView.y = var_titleLabel.bottom + kscale(20)
        var_bvnView.size = CGSizeMake(var_cornerView.width-kscale(40), kscale(48))
        return var_bvnView
    }()
    
    lazy var var_bankCardView: KAVerCodeView = {
        var_bankCardView = KAVerCodeView(placeHolder: "Please enter the bound bank account",limit: 10)
        var_bankCardView.x = kscale(20)
        var_bankCardView.y = var_bvnView.bottom+kscale(16)
        var_bankCardView.size = CGSizeMake(var_cornerView.width-kscale(40), kscale(48))
        return var_bankCardView
    }()
    
    lazy var var_signBtn: UIButton = {
        let var_signBtn = UIButton(frame: CGRectMake(kscale(48), var_bankCardView.bottom+kscale(32), var_cornerView.width-kscale(96), kscale(50.0)))
        var_signBtn.setTitle("Next Step", for: .normal)
        var_signBtn.setTitleColor(.white, for: .normal)
        var_signBtn.titleLabel?.font = UIFont.systemFont(ofSize: kscale(17), weight: .bold)
        var_signBtn.backgroundColor = .themeColor
        var_signBtn.layer.cornerRadius = kscale(25)
        var_signBtn.layer.masksToBounds = true
        var_signBtn.addTarget(self, action: #selector(ka_signButtonClick), for: .touchUpInside)
        return var_signBtn
    }()
}

extension KAUserInfoVerVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        ka_setUI()
//        ka_bindUI()
    }
    
    func ka_setUI() {
        view.backgroundColor = .colorf2f2f5
        
        view.addSubview(var_cornerView)
        var_cornerView.addSubview(var_titleLabel)
        var_cornerView.addSubview(var_bvnView)
        var_cornerView.addSubview(var_bankCardView)

        if status != 2 {
            var_bankCardView.height = 0
        }
        var_cornerView.addSubview(var_signBtn)
    }
    
    @objc func ka_signButtonClick() {
        guard var_bvnView.contentTF.text!.verifyBvn else {
            view.makeToast("Please enter the correct ID number"); return
        }
        if status == 2 {
            guard var_bankCardView.contentTF.text!.count == 10 else {
                view.makeToast("Please Enter a Valid Bank Card Number"); return
            }
        }
        ka_modifyPwd()
    }
    
    @objc func ka_modifyPwd() {
        let mobile = KAUserManager.shared.user.mobile
        let paramDic: [String: Any] = [
            "userGid": KAUserManager.shared.user.userGid,
            "bvn": var_bvnView.contentTF.text!,
            "cardNo": var_bankCardView.contentTF.text!
        ]
        view.showLoading()
        loginProvider.request(.validateLicense(paramDic)) { [self] result in
            view.hideLoading()
            switch result {
            case .success(let response):
                if response.statusCode == 200 {
                    NetResponse<KAContent>.handleResponseData(response.data) { isSuccess, message, response in
                        if isSuccess {
                            guard let content = response?.content else {
                                view.makeToast("Verification failure"); return
                            }
                            if content.status == 0 {
                                view.makeToast(message)
                            }  else if buttonHandle != nil {
                                self.buttonHandle!(var_bvnView.contentTF.text!, var_bankCardView.contentTF.text!)
                            }
                        } else {
                            self.view.makeToast(message)
                        }
                    }
                }else {
                    self.view.makeToast(badNetWorking)
                }
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
}


extension KAUserInfoVerVC: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        view.backgroundColor = .colorf2f2f5
        return view
    }
}


